package com.qf.ps;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer1 {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        //rabbitmq服务器安装的机器IP
        factory.setHost("localhost");
        //rabbitmq通过amqp协议连接时的端口号
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        //设置消息数据保存的虚拟主机,不同的虚拟主机彼此隔离
        factory.setVirtualHost("cdm");

        //获取connection对象
        Connection connection = factory.newConnection();
        //通过connection获取内部通道
        Channel channel = connection.createChannel();

        //消费消息
        channel.basicConsume("psQue1",true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("当前消费者：" + consumerTag);
                System.out.println(new String(body,"utf-8"));
            }
        });

        //消费者启动后，一直处于监听状态，资源不需要关闭
    }
}
